
// 产品模块
const sequelize = require('../config/operateDB');
const {Sequelize, DataTypes, Op} = require('sequelize');
const {User} = require('./users');
const session = require('koa-session');


const News = sequelize.define('news', {
    id: {
        type: DataTypes.UUID,
        allowNull: false,
        primaryKey: true,
        defaultValue: Sequelize.UUIDV4,
    },
    name: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    creatorId: {
        type: DataTypes.UUID,
        allowNull: false,
    },
    content: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
}, {

});

async function initNews() {
    await News.sync();
    // const count = await News.count();
    // if (0 === count) {
    //     await News.create({
    //         id: 'a0a53019-820d-4253-8338-0e6fb09a5e7b',
    //         name: '管理员单位',
    //         logoUrl: '/images/defaultCompany.jpg',
    //         marks: '10',
    //         description: '管理员用户',
    //         cratorName: '管理员',
    //         creator: '6e81b384-dce5-4d58-abf9-56bde96eda25',
            
    //     });

    //     console.log('初始化单位成功！');
    // }
}
initNews();




const newsModel = {
    async getNews(ctx) {
        const params = ctx.query;
        // const count = await News.count();
        const {offset = 0, limit = 10, keywords = '', myAttention} = params;

        let info = {};
        if (1 === myAttention) {
            let attentionIds = ctx.session.sessionData.attentionIds;
            if (attentionIds) {
                attentionIds = attentionIds.split(',');
            }

            info = await News.findAndCountAll(
                {
                    where: {
                        name: {
                            [Op.like]: '%' + keywords + '%',
                        },
                        creatorId: {
                            [Op.in]: attentionIds,
                        }
                    },
                    limit: parseInt(limit),
                    offset: parseInt(offset),
                    raw: true,
                }
            );

        } else {
            info = await News.findAndCountAll(
                {
                    where: {
                        name: {
                            [Op.like]: '%' + keywords + '%',
                        },
                    },
                    limit: parseInt(limit),
                    offset: parseInt(offset),
                    raw: true,
                }
            );
        }
        
        ctx.pageRes({
            limit,
            offset,
            list: info.rows,
            total: info.count,
        });
    },

    async addNews(ctx) {
        const params = ctx.request.body;
        const validFlag = this.validNews(ctx, params);
        if (validFlag) {
            const news = await News.create({
                ...params,
                creatorId: ctx.session.sessionData.id,
            });

            ctx.success('增加成功！', news);
        }
    },
    validNews(ctx, params) {
        if (!params.name) {
            ctx.errorParams('消息名称不存在！');
            return false;
        } else {
            return true;
        }
    },
    async deleteNews(ctx)  {
        if (!!ctx.request.body.id) {
            const deleteIds = (ctx.request.body.id || '').split(',');
            const delCompanies = await News.destroy({
                where: {
                    id: {
                        [Op.in]: deleteIds,
                    },
                },
            });
            if (0 === delCompanies) {
                ctx.errorParams('未找到指定id！');
            } else {
                ctx.success('删除成功！', {
                    id: deleteIds,
                });
            } 
        } else {
            ctx.errorParams('不传id删个得儿啊！');
        }
    },
    async editNews(ctx)  {
        const params = ctx.request.body;
        const editId = params.id;
        if (!editId) {
            ctx.errorParams('id不存在！');
        } else {
            // 
            const news = await News.findOne({
                where: {
                    id: editId
                },
                raw: true,
            });
            await News.update({
                ...news,
                ...params,
            }, {
                where: {
                    id: editId
                },
            });
            ctx.success('修改成功', {
                id: editId,
            });
        }
        
    },
};


module.exports = {
    newsModel,
    News
};